package com.fanghaoran.view.manager;

import com.fanghaoran.controller.ManagerController;
import com.fanghaoran.view.user.BackGroundPanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: win11
 * @Date: 2023/03/29/14:55
 * @Description:
 */
public class BlockFrame extends JFrame {
    private int blockedTime;

    public BlockFrame(String username) throws Exception {
        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView(username);

        //设置可见
        this.setVisible(true);
    }

    private void initView(String username) throws Exception {
        //panel来放组件
        BackGroundPanel panel = new BackGroundPanel(ImageIO.read(new File("src//image//rBackground.jpg")));
        //用垂直box实现组件垂直排列
        Box vBox = Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(100));

        Box box1 = Box.createHorizontalBox();
        JLabel text = new JLabel("选择拉黑的时长");
        box1.add(text);

        Box box2 = Box.createHorizontalBox();
        //查询条件的选择列表
        List<String> selectList = new ArrayList<>();
        selectList.add("3");
        selectList.add("12");
        selectList.add("24");
        selectList.add("48");
        selectList.add("72");
        selectList.add("365*24");
        Vector<String> vector = new Vector<>(selectList);
        JComboBox<String> selector = new JComboBox<>(vector);
        selector.setSelectedIndex(0);
        //设置选择列表显示的行数
        selector.setMaximumRowCount(selectList.size());
        //时间单位文本
        JLabel timeText = new JLabel("小时");
        box2.add(selector);
        box2.add(timeText);

        Box box3 = Box.createHorizontalBox();
        JButton confirmBtn = new JButton("确认");
        box3.add(confirmBtn);

        vBox.add(box1);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box2);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(box3);

        panel.add(vBox);

        this.add(panel);

        //选项条目列表点击事件
        selector.addItemListener(e -> {
                    if (selector.getSelectedItem() != null) {
                        for (String s : selectList) {
                            if (selector.getSelectedItem().equals(s)) {
                                //确认当前要设置的拉黑时间
                                blockedTime = Integer.parseInt(s);
                            }
                        }
                    }
                }
        );
        //确认按钮点击事件
        confirmBtn.addActionListener(e -> {
                    //1.修改该用户在数据库中blocked的值
                    try {
                        new ManagerController().updateBlocked(1L, username);
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                    //2.设置解禁时间(在这段时间用户不能登录)->一段时间后blocked值改回未被拉黑状态
                    //执行限时事件->到了解禁时间就改变blocked
                    ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
                    scheduledExecutorService.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            try {
                                new ManagerController().updateBlocked(0L, username);
                            } catch (SQLException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }, blockedTime, TimeUnit.HOURS);
                    //3.弹框提示,页面关闭
                    JOptionPane.showMessageDialog(this, "拉黑成功");
                    this.dispose();
                }

        );
    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(400, 400);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}
